﻿package bdz.projects.lavender.doi.ui
{
	import bdz.common.display.DisplayLoader;
	import bdz.common.events.GenericEvent;
	import bdz.common.model.AppEnvironment;
	import bdz.common.events.DispatchChannel;
	import com.asual.swfaddress.SWFAddress;
	import com.asual.swfaddress.SWFAddressEvent;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.ExternalInterface;
	import flash.utils.*;
	import flash.ui.ContextMenu;
	import gs.*;
	import gs.easing.*;
	import com.google.analytics.AnalyticsTracker; 
	import com.google.analytics.GATracker; 
	
	/**
	 * ...
	 * @author ...
	 */
	public class DOIMainPreloader extends MovieClip 
	{
		private var display:DisplayLoader;
		private var env:AppEnvironment;
		private var firstRun:Boolean = true;
		private var _currentvalue:String;
		private var _frominternal:Boolean = false;
		private var _loadProgress:Number;
		private var _loadbox:MovieClip;
		private var _tracker:GATracker;
		private var _trackID:String = "UA-10354264-1";
		private var _pagetracker:GATracker;
		public function DOIMainPreloader() {
			addEventListener(Event.ADDED_TO_STAGE, initialize);
		}
		private function organizeStage():void {
			stage.quality = StageQuality.HIGH;
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.addEventListener(Event.RESIZE, onStageResize);
		}
		private function onStageResize(evt:Event = null):void {
			try{
				_loadbox.x = Math.ceil(stage.stageWidth / 2);
				_loadbox.y = Math.ceil(stage.stageHeight / 2);
			}catch (e:Error) {
				
			}
		}
		private function initialize(evt:Event):void {
			organizeStage();
			try{
				_tracker = new GATracker( this, _trackID, "AS3", false ); 
			}catch (e:Error) {
				
			}
			try{
				_pagetracker = new GATracker(this, _trackID, "Bridge", false);
			}catch(e:Error){
			}
			
			
			_loadbox = new LoaderBox();
			_loadbox.alpha = 0;
			_loadbox.scaleX = -3;
			_loadbox.scaleY = 3;
			
			_loadbox.x = Math.ceil(stage.stageWidth / 2);
			_loadbox.y = Math.ceil(stage.stageHeight / 2);
			TweenMax.to(_loadbox, .8, {ease:Strong.easeInOut, scaleX:1, scaleY:1, alpha:1, onComplete:initializeDisplay } );
			addChild(_loadbox);
			
		
		}
		private function initializeDisplay():void {
			display = new DisplayLoader();
			display.addEventListener(ProgressEvent.PROGRESS, onLoadProgress);
			display.addEventListener(Event.COMPLETE, onSiteLoaded);
			display.source = "main.swf";
			env = AppEnvironment.getEnv();
			env.addSetting("baseURL", loaderInfo.parameters.baseURL || "http://info.westpac.com.au/discover_inter/api/");
			
			var cm:ContextMenu = new ContextMenu();
			cm.hideBuiltInItems();
			this.contextMenu = cm;
			_tracker.trackEvent("Page Views", "Preloader");
		}
		
		private function onContentComplete(evt:GenericEvent):void {
			TweenMax.killTweensOf(_loadbox);
			_loadbox.setProgress(1);
			TweenMax.to(_loadbox, .6, {delay:1,  scaleX:-2, scaleY:2, alpha:0, ease:Strong.easeInOut, onComplete:finalizePreloader} );
			display.content.removeEventListener("mainLoadProgress", onMainContentLoadProgress);
			display.content.removeEventListener(GenericEvent.COMPLETE, onContentComplete);
			
			
		}
		private function finalizePreloader():void {
			try{
				removeChild(_loadbox);
			}catch (e:Error) {
				
			}
			stage.removeEventListener(Event.RESIZE, onStageResize);
			SWFAddress.addEventListener(SWFAddressEvent.CHANGE, onAddressChange);
		}
		private function onSiteLoaded(evt:Event):void {
			
			addChild(display);
			display.content.addEventListener("mainLoadProgress", onMainContentLoadProgress);
			display.content.addEventListener(GenericEvent.COMPLETE, onContentComplete);
			display.content.addEventListener("ContentChanged", onContentChanged);
			
			setChildIndex(display, 0);
			
		}
		private function onMainContentLoadProgress(evt:GenericEvent):void {
			if(Number(_loadProgress + Number(evt.data))){
				_loadbox.setProgress(_loadProgress + Number(evt.data));
			}
			
		}
		private function onContentChanged(evt:GenericEvent):void {
			
			_frominternal = true;
			try {
				SWFAddress.setValue(evt.data.shortname);
			
			}catch (e:Error) {
				SWFAddress.setValue(evt.data);
			
			}
			
			//_frominternal = false;
			setTimeout(function() { _frominternal = false; }, 500);
			
			
		}
		private function onAddressChange(evt:SWFAddressEvent):void {
			
			if (!_frominternal) {
					switch(evt.value) {
						case "/":
							display.content["playFromPreloader"]();
							_frominternal = false;
						break;
						case "/home":
							
							if (firstRun) {
								display.content["playFromPreloader"]();
							}else {
								display.content["backToNav"](false);
							}
							_frominternal = false;
						break;
						default:
							
							display.content["gotoContentWithID"](evt.value.substr(1, evt.value.length));
							_frominternal = false;
						break;
					}
					
			}
			if(evt.value.length){
				try{
					//_pagetracker.trackPageview("flash/" + evt.value.substr(1, evt.value.length));
				}catch (e:Error) { }
				
				_tracker.trackEvent("Page Views", evt.value.substr(1, evt.value.length));
			}
			firstRun = false;
			
			
		}
		private function onLoadProgress(evt:ProgressEvent):void {
			_loadProgress = (evt.bytesLoaded / evt.bytesTotal) * .7;
			if (_loadProgress > 0 || _loadProgress == 0) {
				if(Number(_loadProgress)){
					_loadbox.setProgress(_loadProgress);
				}
			}
			
		}
		
	}
	
}